﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// 部门设定画面
/// </summary>
public partial class SystemManage_DepartMentManageEdit : BasePage
{

    #region 画面初始化

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //添加部门
            if (Request.QueryString["Action"] == "Add")
            {
                SetControlsState(enumAction.InsertAction);
            }
            //更新部门
            else if (Request.QueryString["Action"] == "Update")
            {
                SetControlsState(enumAction.UpdateAction);
            }
            //更新部门
            else if (Request.QueryString["Action"] == "Del")
            {
                SetControlsState(enumAction.DeleteAction);
            }
        }
    }

    #endregion

    #region 画面方法

    private void SetControlsState(enumAction enumAction)
    {
        switch (enumAction)
        {
            case enumAction.InsertAction:
                txtComCode.Text.Trim();
                txtComName.Text.Trim();
                break;
            case enumAction.UpdateAction:
                GH.BLL.DepartMent departmentBll = new GH.BLL.DepartMent();
                GH.Model.部门信息 mdlUnionInfo = new GH.Model.部门信息();
                mdlUnionInfo = departmentBll.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                txtComCode.Text = mdlUnionInfo.部门代码;
                txtComName.Text = mdlUnionInfo.部门名称;
                break;
            case enumAction.DeleteAction:
                UnionOpration();
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 部门登录,更新,删除
    /// </summary>
    public void UnionOpration()
    {
        GH.BLL.DepartMent departmentBll = new GH.BLL.DepartMent();
        GH.Model.部门信息 mdlDepartMentInfo = new GH.Model.部门信息();
        mdlDepartMentInfo.部门代码 = txtComCode.Text.Trim();
        mdlDepartMentInfo.部门名称 = txtComName.Text.Trim();

        //添加部门
        if (Request.QueryString["Action"] == "Add")
        {
            try
            {
                mdlDepartMentInfo.单位ID = int.Parse(Request.QueryString["UnionID"].ToString());
                departmentBll.Add(mdlDepartMentInfo);
                AlertMessage("添加部门成功");
                JavascriptGoUrl("DepartMentManage.aspx");
            }
            catch
            {
                AlertMessage("添加部门失败");
            }
        }
        //更新部门
        else if (Request.QueryString["Action"] == "Update")
        {
            try
            {
                mdlDepartMentInfo.ID = int.Parse(Request.QueryString["ID"].ToString());
                departmentBll.Update(mdlDepartMentInfo);
                AlertMessage("更新部门成功");
                JavascriptGoUrl("DepartMentManage.aspx");
            }
            catch
            {
                AlertMessage("更新部门失败");
            }

        }
        //删除部门
        else if (Request.QueryString["Action"] == "Del")
        {
            try
            {
                mdlDepartMentInfo.ID = int.Parse(Request.QueryString["ID"].ToString());
                departmentBll.Delete(mdlDepartMentInfo.ID);
                AlertMessage("删除部门成功");
                //JavascriptGoBack();
                Response.Redirect(Request.ServerVariables["http_referer"]);
            }
            catch
            {
                AlertMessage("删除部门失败");
                JavascriptGoBack();
            }
        }
    }


    #endregion

    #region 画面事件

     protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        UnionOpration();
    }

    #endregion
}
